package com.srunpass.algorithm;

/**
 * @author srunpass
 * @version Id: N的第k个字典值, v 0.1 2025/6/9 10:49 srunpass Exp $
 */
public class N的第k个字典值 {
    static int count = 1;

    public static int solution(int n, int k) {
        long cnt=0;
        int num=1;
        while (true){
            if(cnt==k-1){
                return num;
            }
            long tmp = count(num,n);
            if(cnt+tmp<k){
                num++;
                cnt+=tmp;
            }else {
                cnt++;
                num*=10;
            }
        }

    }

    static long count(long num, int n) {
        long cnt = 1;
        long width = 1;
        while (true) {
            if (num + width < n) {
                cnt += width;
                num *= 10;
                width *= 10;
            } else {
                if (num <= n) {
                    cnt += num - n + 1;
                }
                break;
            }
        }
        return cnt;
    }

    public static void main(String[] args) {
        System.out.println(solution(14, 7));
    }

}
